package com.syh.jn.scm.infrastructure.repository.impl.downstream.finance;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.downstream.entity.finance.DownPaymentBill;
import com.syh.jn.scm.domain.downstream.repository.finance.DownPaymentBillRepository;
import com.syh.jn.scm.dto.downstream.finance.DownPaymentBillVo;
import com.syh.jn.scm.infrastructure.repository.model.finance.ReceiptBillModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * Date:2018/7/19
 * Author:Clown
 * Desc;
 */
@Repository
public class DownPaymentBillRepositoryImpl extends AbstractAggregateRepository<DownPaymentBill, ReceiptBillModel, Long> implements DownPaymentBillRepository {

    @Override
    protected SQL getViewSql() {
        return new SQL() {{
            SELECT(
                    SQLAliasGenerateUtil.aliasGenerate(ReceiptBillModel.class,"receiptBill",null),
                    "statementAccount.CODE AS statementOfAccountCode"
            );
            FROM("T_FINANCE_RECEIPT_BILL receiptBill");
            INNER_JOIN(
                    "T_FINANCE_STATEMENT_ACCOUNT_BILL statementAccount ON statementAccount.id = receiptBill.STATEMENT_OF_ACCOUNT_ID"
            );
        }};
    }

    @Override
    protected Class getViewObject() {
        return DownPaymentBillVo.class;
    }

    @Override
    protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
        Map<String, String> params = new HashMap<>();
        params.put("id", "receiptBill.ID");
        params.put("customerId", "receiptBill.CUSTOMER_ID");
        params.put("customerType", "receiptBill.CUSTOMER_TYPE");
        params.put("statementOfAccountId", "receiptBill.STATEMENT_OF_ACCOUNT_ID");
        params.put("statementOfAccountCode", "statementAccount.CODE");
        params.put("confirmState", "receiptBill.CONFIRM_STATE");
        params.put("businessEntityId", "receiptBill.BUSINESS_ENTITY_ID");
        params.put("receiptState", "receiptBill.RECEIPT_STATE");
        params.put("otherPartyAccountBankCode", "receiptBill.OTHER_PARTY_ACCOUNT_BANK_CODE");
        params.put("code", "receiptBill.CODE");
        params.put("statementOfAccountCode", "statementAccount.CODE");
        ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
            return params.get(propertyName);
        });
        return parse;
    }

}
